package com.jye.cloud.user.service.impl;



import com.jye.cloud.Constants.MyConstants;
import com.jye.cloud.domain.*;
import com.jye.cloud.user.mapper.*;
import com.jye.cloud.user.service.CoursesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class CoursesServiceImpl implements CoursesService {


    @Autowired
    CoursesMapper coursesMapper;


    @Autowired
    UserMapper userMapper;

    @Autowired
    CommentsMapper commentsMapper;

    @Autowired
    ClassMapper classMapper;

    @Autowired
    LikeMapper likeMapper;

    @Autowired
    FollowMapper followMapper;


    @Override
    public Courses findCourseByIdAndUId(int id, int uId) {
        Courses courses = coursesMapper.findCourseById(id);

        //常量类别
        int lType = MyConstants.likeType.coursesType;
        //点赞数
        Like like = new Like();
        like.setType(lType);
        like.setUId(uId);
        like.setPId(id);
        like.setStatus(MyConstants.isLikeOrFollow.yes);//为点赞了
        int count = likeMapper.findLikeCountByLike(like);
        courses.setLike(count);
        //是否点赞
        int likeExist = likeMapper.isLike(like);
        if (likeExist == 0) {
            //未点赞
            courses.setIslike(false);
        } else {
            //点赞了
            courses.setIslike(true);
        }

        //是否关注
        int fType = MyConstants.followType.courseType;
        Follow follow = new Follow();
        follow.setType(fType);
        follow.setPId(id);
        follow.setUId(uId);
        follow.setStatus(MyConstants.isLikeOrFollow.yes);
        int followExist = followMapper.isFollow(follow);
        if (followExist == 0) {
            courses.setIsfollow(false);
        } else {
            courses.setIsfollow(true);
        }

        //是否拥有课程
        int havaCourses = coursesMapper.haveCourse(id, uId);
        if (havaCourses == 0) {
            courses.setIscourses(false);
        } else {
            courses.setIscourses(true);
        }


        return courses;
    }

    @Override
    public List<Courses> findAllCourses(int skip, int limit) {
        return coursesMapper.findAllCourses(skip, limit);
    }

    @Override
    public int addCourse(Courses courses) {
        try {
            coursesMapper.addCourse(courses);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public int updateCourse(Courses courses) {
        try {
            coursesMapper.updateCourse(courses);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }



    @Override
    public List<Courses> findHotCourses(int uId, int skip, int limit) {
        //常量类别
        int type = MyConstants.likeType.coursesType;
        List<Like> likes = likeMapper.findHotLikeByType(type, skip, limit);
        List<Courses> coursesList = new ArrayList<>();
        Courses courses;
        for (Like like : likes) {
            //查找课程
            courses = coursesMapper.findCourseById(like.getPId());
            //设置点赞数量
            courses.setLike(like.getCount());
            /*//是否点赞
            int flag = likeMapper.isLikeByUIdAndPId(uId, courses.getId());
            if(flag==0){
                courses.setIslike(false);
            }else{
                courses.setIslike(true);
            }*/

            //加进List
            coursesList.add(courses);
        }
        return coursesList;
    }

    @Override
    public List<Courses> findMyCoursesByUId(int uId, int skip, int limit) {
        //        List<Courses> list = coursesMapper.findCoursesByUserId(user_id);

        List<Integer> cIds = coursesMapper.findMyCoursesByUId(uId, skip, limit);
        List<Courses> list = new ArrayList<Courses>();
        Courses courses = new Courses();
        for (Integer cId : cIds) {
            courses = coursesMapper.findCourseById(cId);
            list.add(courses);
        }


        return list;
    }

    @Override
    public List<Courses> findTeacherCoursesByUId(int uId) {
        List<Integer> cIds = coursesMapper.findTeacherCoursesByUId(uId);
        List<Courses> coursesList = new ArrayList<>();
        for (Integer cId : cIds) {
            Courses courses = coursesMapper.findCourseById(cId);
            //添加课程
            coursesList.add(courses);
        }
        return coursesList;
    }

    @Override
    public List<User> findCourseUserDetailByCId(int cId) {
        //查询有该课程的用户id集合
        List<Integer> uIds = coursesMapper.findCourseUserIdByCId(cId);

        //该课程下分班id集合
        List<Integer> classIds = classMapper.findClassIdsByCId(cId);

        //用户list
        List<User> userList = new ArrayList<>();

        for (Integer uId : uIds) {
            //找出user
            User user = userMapper.findUserById(uId);

            //是否分班
            for (int classId : classIds) {
                //是否在班级
                int inClass = classMapper.isInClass(uId, classId);
                //在
                if (inClass != 0) {
                    //找到名字,设置
                    user.setClassName(classMapper.findClassNameById(classId));
                    break;
                }
            }

            //增加user
            userList.add(user);
        }
        return userList;
    }

    @Override
    public List<User> findCoursesNotClassUserById(int id) {
        //查询有该课程的用户id集合
        List<Integer> uIds = coursesMapper.findCourseUserIdByCId(id);

        //班级id
        List<Integer> cIds = classMapper.findClassIdsByCId(id);

        //用户list
        List<User> userList = new ArrayList<>();

        Boolean flag;
        for (Integer uId : uIds) {
            flag = true;
            for (Integer cId : cIds) {
                //是否分班
                int i = classMapper.isInClass(uId, cId);
                if( i != 0){
                    //已分班
                    flag = false;
                    break;
                }
            }
            if(flag){
                User user = userMapper.findUserById(uId);
                userList.add(user);
            }

        }

        return userList;
    }

    @Override
    public List<Courses> findNewestCourses(int skip, int limit) {
        List<Courses> newestCourses = coursesMapper.findNewestCourses(skip, limit);
        return newestCourses;
    }

    @Override
    public List<Courses> findCoursesByType(String type, int skip, int limit) {
        List<Courses> coursesList = coursesMapper.findCoursesByType(type, skip, limit);
        int likeType = MyConstants.likeType.coursesType;
        Like like = new Like();
        like.setType(likeType);
        int likeCount;
        for (Courses courses : coursesList) {
            like.setPId(courses.getId());
            likeCount = likeMapper.findLikeCountByLike(like);
            courses.setLike(likeCount);
        }
        return coursesList;
    }

    @Override
    public String userBuyCourse(int uId, int cId) {
        try{
            coursesMapper.userBuyCourse(uId,cId);
            return "购买成功!如显示未拥有请刷新页面";
        }catch (Exception e){
            return "购买失败!请确认你是否已有该课程!勿重复购买!";
        }
    }


}
